/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.hslu.prg2.team2.logic;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Claudio
 */
public class GameboardSerializer
{
    public void serialize(Gameboard gameboard, String filePath )
    {      
        try
        {
            try (FileOutputStream fileOutputStream = new FileOutputStream(filePath); 
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream))
            {
                objectOutputStream.writeObject(gameboard);
            }
        } 
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(GameboardSerializer.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IOException ex)
        {
            Logger.getLogger(GameboardSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Gameboard deserialize(String filePath)
    {
        try
        {
            try (FileInputStream fileInputStream = new FileInputStream(filePath); 
                    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream))
            {
                Gameboard gameboard = (Gameboard)objectInputStream.readObject();
                return gameboard;
            }
        } 
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(GameboardSerializer.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IOException | ClassNotFoundException ex)
        {
            Logger.getLogger(GameboardSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }
}
